package com.spit.hkpost.adapter

import android.widget.Filter
import android.widget.Filterable
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.spit.hkpost.R
import com.spit.hkpost.bean.DataBean
import com.spit.hkpost.bean.db.RfidBean
import com.spit.hkpost.ext.setAdapterAnimation
import com.spit.hkpost.util.SettingUtil

/**
 * User: Nike
 *  2023/12/15 16:12
 */
class RfidListAdapter(data: ArrayList<DataBean>, pageStu: Int) :
    BaseQuickAdapter<DataBean, BaseViewHolder>(R.layout.i_rfid_list, data), Filterable {

    private var mPageStatus: Int = 0

    init {
        setAdapterAnimation(SettingUtil.getListMode())
        this.mPageStatus = pageStu
    }

    override fun convert(holder: BaseViewHolder, item: DataBean) {
        val bean = mFilterList[holder.layoutPosition]

        holder.setText(R.id.tv_id, String.format("%06d", holder.layoutPosition + 1))
//        holder.setText(R.id.tv_id, String.format("%06d", bean.uid))
        holder.setText(R.id.tv_epc, bean.tagId)
        holder.setText(R.id.tv_date, bean.crearDate)
        holder.setVisible(R.id.fy_sel, if (bean.status == 1 && mPageStatus == 0) true else false)
    }

    var mFilterList = ArrayList<DataBean>()

    fun appendList(list: List<DataBean>) {
        data = list as MutableList<DataBean>
        //这里需要初始化filterList
        mFilterList = list as ArrayList<DataBean>
    }

    override fun getFilter(): Filter {
        return object : Filter() {
            //执行过滤操作
            override fun performFiltering(charSequence: CharSequence): FilterResults {
                val charString = charSequence.toString()
                if (charString.isEmpty()) {
                    //没有过滤的内容，则使用源数据
                    mFilterList = data as ArrayList<DataBean>
                } else {
                    val filteredList: MutableList<DataBean> = ArrayList()
                    for (i in data.indices) {
                        val bean = data[i]
                        if (bean.tagId!!.contains(charString, ignoreCase = true) == true) {
                            filteredList.add(bean)
                        }
                    }
                    mFilterList = filteredList as ArrayList<DataBean>
                }
                val filterResults = FilterResults()
                filterResults.values = mFilterList
                return filterResults
            }

            //把过滤后的值返回出来
            override fun publishResults(charSequence: CharSequence, filterResults: FilterResults) {
                mFilterList = filterResults.values as ArrayList<DataBean>
                notifyDataSetChanged()
            }
        }
    }

    override fun getItemCount(): Int {
        return mFilterList.size
    }

}